﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entities;
using Datalayer;
using Common;
using System.Data;

namespace Portal_KonTum.Admin
{
    public partial class CatalogyMn : System.Web.UI.Page
    {
        IQueryMgr iq;
        protected void Page_Load(object sender, EventArgs e)
        {
            notes();
            if (!IsPostBack)
            {
                bin();
            }
        }
        void notes()
        {
            if (Request.QueryString["Notes"] != null && Request.QueryString["Active"] != null)
            {
                string snotes = Request.QueryString["Notes"];
                string sactive = Request.QueryString["Active"];
                string sout = MethodCommon.StrQueryNotes(snotes, sactive);
                lbnotes.Visible = true;
                lbnotes.InnerText = sout;
            }
        }
        void bin()
        {
            List<Category> ex = new List<Category>();
            iq = QueryMgr.GetDbInstance();
            ex = iq.Ca_GetAllCate_NotParent();
            
            GridView1.DataSource = CreateCategory(ex,"+","  -");
            GridView1.DataBind();
        }
        DataTable CreateCategory(List<Category> ex ,string DauParent, string DauParent2)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("CateID", typeof(Guid));
	        dt.Columns.Add("CategoryName", typeof(string));
            dt.Columns.Add("STT", typeof(string));
            int I1 = 0;
            IQueryMgr iq = QueryMgr.GetDbInstance();
            foreach(Category obj in ex)
            {
                List<Category> ListCateParent = new List<Category>();
                ListCateParent = iq.Ca_GetByCateParentID(obj.CateID);
                if (ListCateParent.Count == 0)
                {
                    I1++;
                    dt.Rows.Add(obj.CateID, obj.CategoryName, I1);
                }
                else
                {
                    I1++;
                    dt.Rows.Add(obj.CateID, obj.CategoryName,I1);
                    int I2 = 1;
                    foreach (Category CateParent in ListCateParent)
                    {
                        dt.Rows.Add(CateParent.CateID, DauParent + CateParent.CategoryName, I1 + "." + I2++);

                        List<Category> ListCateParent2 = new List<Category>();
                        ListCateParent2 = iq.Ca_GetByCateParentID(CateParent.CateID);
                        if (ListCateParent.Count != 0)
                        {
                            int I3 = 1;
                            foreach (Category CateParent2 in ListCateParent2)
                            {
                                dt.Rows.Add(CateParent2.CateID, DauParent2 + CateParent2.CategoryName, I1 + "." + I2 + "." + I3++);
                            }
                        }
                    }
                }
            }
            return dt;
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //HiddenField hfs = (HiddenField)e.Row.FindControl("hf_cb");
                //Label lb_cs = (Label)e.Row.FindControl("lb_cb");
                //HyperLink lb_cs = (HyperLink)e.Row.FindControl("HyperLink1");
                //iq = QueryMgr.GetDbInstance();
                //if (hfs.Value != null)
                //{
                //    Category st = new Category();
                //    st = iq.Ca_GetCateByID(MethodCommon.Get_Guid(hfs.Value));
                //    lb_cs.Text = st.CategoryName + "";
                //}
            }
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            iq = QueryMgr.GetDbInstance();
            Category cus = new Category();
            String index = e.CommandArgument.ToString();

            if (e.CommandName == "rowEdit")
            {
                Response.Redirect("CatalogyNew.aspx?CateID=" + index);
            }
            if (e.CommandName == "rowDelete")
            {
                Guid _id = new Guid(index);
                iq = QueryMgr.GetDbInstance();
                iq.Ca_DelOrResCategory(_id);
                bin();
                Response.Redirect("CatalogyMn.aspx?Notes=StrDel&Active=true");
            }
        }

        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            bin();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;

            GridView1.EditIndex = -1;
            GridView1.SelectedIndex = -1;
        }
    }
}